package util.crypto

import java.security.SecureRandom
import util.HexaUtils

/**
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: 28.2.11
 * Time: 16:58
 * To change this template use File | Settings | File Templates.
 */

class Nonce(val nonceLength: Int = 64 / 8, val seedByteCount: Int = 10) {

  // Create a secure random number generator
  // Create two secure number generators with the same seed
  private[this] val seedRandom = SecureRandom.getInstance("SHA1PRNG")
  private[this] val seed = seedRandom.generateSeed(10)

  private[this] val nonceRandom = {
    val rnd = SecureRandom.getInstance("SHA1PRNG")
    rnd.setSeed(seed)
    rnd
  }

  def generateNonce(): String = {
    val nonceBytes: Array[Byte] = new Array(nonceLength)
    nonceRandom.nextBytes(nonceBytes)
    HexaUtils.byteArrayToHexString(nonceBytes)
  }

}